package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;

import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleImplService implements IRoleService {
   @Autowired
    private RoleMapper roleMapper;

    public void save (Role role, Long[] ids) {
        roleMapper.insert(role);

        if (ids != null && ids.length > 0) {
            roleMapper.insertBatchRelation(role.getId(),ids);
        }
    }

    public void updateById (Role role, Long[] ids) {
        roleMapper.updateByPrimaryKey(role);

        roleMapper.deleteRelation(role.getId());

        if (ids != null && ids.length > 0) {
            roleMapper.insertBatchRelation(role.getId(),ids);
        }
    }

    public void deleteById (Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    public Role selectById (Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }


    public List<Role> listAll () {
        return roleMapper.selectAll();
    }

    public PageInfo<Role> query (QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        List<Role> result = roleMapper.queryForList(qo);

        return new PageInfo<>(result);
    }


}
